import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { knife4jSetup } from 'nest-knife4j';
import { NestExpressApplication } from '@nestjs/platform-express';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.setGlobalPrefix('api');
  app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
  app.useGlobalPipes(new ValidationPipe());
  app.enableCors();
  app.useStaticAssets('uploads', { prefix: '/uploads' });

  if (process.env.NODE_ENV !== 'production') {
    const config = new DocumentBuilder()
      .addBearerAuth({
        type: 'http',
        description: '基于 jwt 的认证',
      })
      .setTitle('低代码')
      .setDescription('api 接口文档')
      .setVersion('1.0')
      .addTag('lowcode')
      .build();
    const document = SwaggerModule.createDocument(app, config);
    SwaggerModule.setup('api', app, document);
    // http://localhost:3000/doc.html
    knife4jSetup(app, [
      {
        name: '2.X版本',
        url: `/api-json`,
        swaggerVersion: '2.0',
        location: `/api-json`,
      },
    ]);
  }
  await app.listen(3000);
}
bootstrap();
